package com.wjjun.basemvpvm.utils;

import android.text.InputFilter;
import android.text.Spanned;

/**
 * 限制最大值和最小值
 */
public class RangeInputFilter implements InputFilter {

    private double maxValue = Integer.MAX_VALUE - 1;
    private double minValue = 0;

    public RangeInputFilter() {
    }

    public RangeInputFilter(double maxValue, double minValue) {
        this.maxValue = maxValue;
        this.minValue = minValue;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        try {
            double value = Double.parseDouble(dest.toString() + source.toString());
            if (value >= minValue && value <= maxValue) {
                return null;
            } else {
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public double getMaxValue() {
        return maxValue;
    }

    public void setMaxValue(double maxValue) {
        this.maxValue = maxValue;
    }

    public double getMinValue() {
        return minValue;
    }

    public void setMinValue(double minValue) {
        this.minValue = minValue;
    }
}